package com.erp.module.faHuoDanJu.faHuo2

import android.app.Activity
import android.content.Intent
import android.util.Log
import android.view.View
import androidx.appcompat.app.AlertDialog
import com.erp.R
import com.erp.activity.BaseActivity
import com.erp.bean.FaHuoBean
import com.erp.huadonglistview.DragSortListView
import com.erp.module.faHuoDanJu.FaHuoDanJuContract
import com.erp.myAdapter.FaHuoKAdapter
import com.erp.myAdapter.FaHuoOnumAdapter
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_fahuo_car.*
import kotlinx.android.synthetic.main.dialog_onum_sort.LvOnumSort
import kotlinx.android.synthetic.main.dialog_onum_sort.tv_quite_cancel
import kotlinx.android.synthetic.main.dialog_onum_sort.tv_quite_confirm
import kotlinx.android.synthetic.main.header_knoya.*

/**
 * Created by Administrator on 2018/2/5/005.
 * 作者：刘洋
 * 单据上传界面
 */

class FaHuoCarAvtivity : BaseActivity(), FaHuoDanJuContract.FaHuoCarKView, View.OnClickListener {

    lateinit var dialog: AlertDialog
    lateinit var fahuobean: FaHuoBean
    lateinit var adapter : FaHuoKAdapter
    var CarNumble =""
    lateinit var KP : IFaHuoCarKP

    // 监听器在手机拖动停下的时候触发
    private val onDrop = DragSortListView.DropListener { from, to ->
        Log.d("onDrop","onDrop="+"onDrop")
        if (from != to) {
            val item = adapter.getItem(from) as FaHuoBean.shop// 得到listview的适配器
            adapter.remove(from)// 在适配器中”原位置“的数据。
            adapter.insert(item, to)// 在目标位置中插入被拖动的控件。
        }
    }
    // 删除。
//    private val onRemove = DragSortListView.RemoveListener { which -> adapter.remove(which) }

    override fun getLayoutId(): Int {
        return R.layout.activity_fahuo_car
    }

    override fun initCreate() {
        CarNumble=intent.getStringExtra("CarNumBle")
        fahuobean=FaHuoBean()
        KP= IFaHuoCarKP(this, this, fahuobean)
        adapter= FaHuoKAdapter(this@FaHuoCarAvtivity, this, fahuobean.shops)
        LvFaHuo.setDropListener(onDrop)
//        LvFaHuo.setRemoveListener(onRemove)
        LvFaHuo.adapter=adapter
        LvFaHuo.isDragEnabled = true
        dialog = Myutil.showupdatedialog(this@FaHuoCarAvtivity, "正在获取数据", false)
        CarShuJu()
        iv_header_left.visibility=View.VISIBLE
        iv_header_left.setOnClickListener(this)
        tv_header_title.text="物流单据"
        tv_header_right.visibility=View.VISIBLE
        tv_header_right.text = "保存排序"
        tv_header_right.setOnClickListener(this)
    }

    fun CarShuJu(){
        KP.SouSuoCar(CarNumble)
    }


    override fun SouSuoCarOk() {
        dialog.dismiss()
        Myutil.settv(fahuobean.car_name,TvCarName)
        Myutil.settv(fahuobean.location,TvLocation)
        Myutil.settv(fahuobean.car_number,TvCarNumble)
        Myutil.settv(fahuobean.fullcarload,TvFullcarload)
        Myutil.settv(Myutil.getdata(fahuobean.plan_deliver_time),TvPlanDeliverTime)
        Myutil.settv(fahuobean.remark,Tvremark)
        adapter.notifyDataSetChanged()
    }

    override fun SouSuoCarNo() {
        dialog.dismiss()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null) {
            return
        }
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                50 -> {
                    dialog = Myutil.showupdatedialog(this@FaHuoCarAvtivity, "正在刷新数据", false)
                    CarShuJu()
                }
            }
        }
    }

    override fun onClick(v: View?) {
        when(v){
            iv_header_left -> {
                finish()
            }
            tv_header_right -> {
                dialog = Myutil.showupdatedialog(this@FaHuoCarAvtivity, "正在更改排序", false)
                KP.postshopweight()
            }
        }
    }

    override fun dowZhuanMaiDian(position: Int) {
//        val s = fahuobean.shops[position].onums.replace(",","\n")
//        val dialog = AlertDialog.Builder(this)
//        dialog.setTitle("单号")
//        dialog.setMessage(s)
//        dialog.show()
        var onumadapter= FaHuoOnumAdapter(this@FaHuoCarAvtivity, this, fahuobean.shops[position].onums)
        val onOnumDrop = DragSortListView.DropListener { from, to ->
            Log.d("onDrop","onDrop="+"onDrop")
            if (from != to) {
                val item = onumadapter.getItem(from) as FaHuoBean.onum// 得到listview的适配器
                onumadapter.remove(from)// 在适配器中”原位置“的数据。
                onumadapter.insert(item, to)// 在目标位置中插入被拖动的控件。
            }
        }
        val quitdialog = AlertDialog.Builder(this).create()
        quitdialog.show()
        quitdialog.window!!.setContentView(R.layout.dialog_onum_sort)
        val LvOnumSort = quitdialog.LvOnumSort
        LvOnumSort.setDropListener(onOnumDrop)
//        LvFaHuo.setRemoveListener(onRemove)
        LvOnumSort.adapter=onumadapter
        LvOnumSort.isDragEnabled = true

        val tv_quite_confirm = quitdialog.tv_quite_confirm
        val tv_quite_cancel = quitdialog.tv_quite_cancel
        quitdialog.setCanceledOnTouchOutside(false)
        tv_quite_cancel.setOnClickListener { quitdialog.dismiss() }
        tv_quite_confirm.setOnClickListener {
            dialog = Myutil.showupdatedialog(this@FaHuoCarAvtivity, "正在更改排序", false)
            KP.postonumweight(position)
            quitdialog.dismiss()
        }
    }

//    override fun dowPhoto(position: Int) {
//        val myAlerDialog = MyAlerDialog(this@LingShi4Avtivity)
//        myAlerDialog.start()
//        myAlerDialog.setdata("请选择")
//        myAlerDialog.setright("物流单据") {
//            photo=fahuobean.shops[position].dvlist
//            val intent = Intent(this@LingShi4Avtivity, LingShi3Avtivity::class.java)
//            intent.putExtra("CarNumber",fahuobean.car_number)
//            intent.putExtra("ShopName",fahuobean.shops[position].shop_name)
//            intent.putExtra("type", WuLiu)
//            startActivityForResult(intent,50)
//            myAlerDialog.dismiss()
//        }
//        myAlerDialog.setleft("发货清单") {
//            photo=fahuobean.shops[position].dllist
//            val intent = Intent(this@LingShi4Avtivity, LingShi3Avtivity::class.java)
//            intent.putExtra("CarNumber",fahuobean.car_number)
//            intent.putExtra("ShopName",fahuobean.shops[position].shop_name)
//            intent.putExtra("type", FaHuo)
//            startActivityForResult(intent,50)
//            myAlerDialog.dismiss()
//        }
//        myAlerDialog.show()
//    }
    /**点击物流*/
    override fun downWuliu(position: Int) {
        if(fahuobean.shops[position].shop_name==""){
            Myutil.MyTost("专卖店不能为空")
            return
        }
        photo =fahuobean.shops[position].dvlist
        val intent = Intent(this@FaHuoCarAvtivity, DanJuPaiZhaoShangChuanAvtivity::class.java)
        intent.putExtra("CarNumber",fahuobean.car_number)
        intent.putExtra("ShopName",fahuobean.shops[position].shop_name)
        intent.putExtra("type", WuLiu)
        startActivityForResult(intent,50)
    }
    /**点击发货*/
    override fun downFahuo(position: Int) {
        if(fahuobean.shops[position].shop_name==""){
            Myutil.MyTost("专卖店不能为空")
            return
        }
        photo =fahuobean.shops[position].dllist
        val intent = Intent(this@FaHuoCarAvtivity, DanJuPaiZhaoShangChuanAvtivity::class.java)
        intent.putExtra("CarNumber",fahuobean.car_number)
        intent.putExtra("ShopName",fahuobean.shops[position].shop_name)
        intent.putExtra("type", FaHuo)
        startActivityForResult(intent,50)
    }

    companion object {
        val WuLiu = 1
        val FaHuo = 2
        var photo : MutableList<FaHuoBean.photo> = ArrayList()
    }
}
